package app.softtech.freechat;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import app.softtech.freechat.adapter.TabsPaperAdapter;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
	static final String TAG = MainActivity.class.getSimpleName();

	private ViewPager mViewPaper;
	private ActionBar mActionBar;
	private TabsPaperAdapter mAdapter;

	private String[] tab = { "Friend List", "Recent" };
	String resultSearch = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Initilization
		mViewPaper = (ViewPager) findViewById(R.id.pager);
		mActionBar = getActionBar();
		mAdapter = new TabsPaperAdapter(getSupportFragmentManager());

		mViewPaper.setAdapter(mAdapter);
		mActionBar.setHomeButtonEnabled(false);
		mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		// Adding Tabs
		for (String tabName : tab) {
			mActionBar.addTab(mActionBar.newTab().setText(tabName).setTabListener(this));
		}

		mViewPaper.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				mActionBar.setSelectedNavigationItem(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.main, menu);
//
//		SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
//		SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
//		searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
//
//		searchView.setOnQueryTextListener(new OnQueryTextListener() {
//
//			@Override
//			public boolean onQueryTextSubmit(String query) {
//				return false;
//			}
//
//			@Override
//			public boolean onQueryTextChange(String newText) {
//				Log.i(TAG, newText);
//				return false;
//			}
//		});
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		mViewPaper.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {

	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {

	}
}
